package com.ymatou.userregister.testcase;

import java.util.Date;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userregister.parameter.AliUserRegBean;
import com.ymatou.userregister.parameter.BindEmailPhoneBean;
import com.ymatou.userregister.service.AliUserRegCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserRegisterService;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

public class Ts_AliUserReg {
	private static AliUserRegBean aliuserregbean;
	private static AliUserRegCall aliuserregCall;
	private static YmtUsersWapper userWapper; 
	private static UserTokenWapper userTokenWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建阿里用户接口");
	}

	@Before
	public void caseUp() throws Exception {
		aliuserregbean=new AliUserRegBean();
		aliuserregCall=new AliUserRegCall();
		userWapper = new YmtUsersWapper();
		userTokenWapper = new UserTokenWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(aliuserregCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserReg_001() {
		Logger.start(true,"创建用户成功");
		try {
			String aliUserId = TestDataManager.getRandomNum(7);
			
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("Lily");
			aliuserregbean.setSEmail("12456@123.com");
			// setRegSource只能取1,2,6,7
			aliuserregbean.setRegSource(0);
			aliuserregbean.setTerminalSource("mainsite");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("注册成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Thread.sleep(1000);
			Map user = userWapper.selectUserAtAliByAliId(aliUserId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			
			Logger.verifyEquals(userToken.get("uId"), aliuserregCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, aliuserregCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserReg_002() {
		Logger.start(true,"用户已存在，则用户直接登陆成功");
		try {
			aliuserregbean.setSAliUserId("aliId1");
			aliuserregbean.setSRealName("Lily");
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(0);
			aliuserregbean.setTerminalSource("App");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("登录成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Map user = userWapper.selectUserAtAliByAliId("aliId1");
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			
			Logger.verifyEquals(userToken.get("uId"), aliuserregCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyIsNull( aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, aliuserregCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AliUserReg_003() {
		Logger.start(false,"不输入Ali用户Id，用户创建失败");
		try {
			aliuserregbean.setSRealName("Lily");
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(0);
			aliuserregbean.setTerminalSource("App");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("dto验证失败", aliuserregCall.getString("LastErrorMessage"), "验证返回的Message信息");
			Logger.verifyEquals(-1d, aliuserregCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyIsNull(aliuserregCall.getResult().get("Token"), "验证Token");
			Logger.verifyIsNull(aliuserregCall.getResult().get("Remark"), "验证remark信息");
			Logger.verifyEquals(29d, aliuserregCall.getResult().get("VerifyCode"), "验证凭据(29表示阿里Id为空)");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AliUserReg_004(){
		Logger.start(true,"不输入真实用户名，用户创建成功");
		try {
			String aliUserId = TestDataManager.getRandomNum(7);
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSEmail(TestDataManager.getRandomNum(7)+"@Ali.com");
			aliuserregbean.setRegSource(0);
			aliuserregbean.setTerminalSource("App");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();	
			
			Logger.verifyEquals("注册成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Thread.sleep(1000);
			Map user = userWapper.selectUserAtAliByAliId(aliUserId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			
			Logger.verifyEquals(userToken.get("uId"), aliuserregCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, aliuserregCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AliUserReg_005(){
		Logger.start(true,"不输入email，用户创建成功");
		try {
			String aliUserId = TestDataManager.getRandomNum(7);
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setRegSource(3);
			aliuserregbean.setTerminalSource("App");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();	
			
			Logger.verifyEquals("注册成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Map user = userWapper.selectUserAtAliByAliId(aliUserId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			
			Logger.verifyEquals(userToken.get("uId"), aliuserregCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, aliuserregCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}
